package com.shigu.address.service;

import com.opentae.data.mall.beans.MallUserAddr;
import com.opentae.data.mall.examples.MallUserAddrExample;
import com.opentae.data.mall.interfaces.MallUserAddrMapper;
import com.shigu.PageResponse;
import com.shigu.address.vo.AddressDetailResponse;
import com.shigu.address.vo.AddressResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressService {
    @Autowired
    MallUserAddrMapper mallUserAddrMapper;

    /**
     *获取地址列表
     * @author 郭尔
     * @date 2018/10/25 10:01
     */
    public PageResponse<AddressResponse> addressList(Long userId){
        PageResponse<AddressResponse> response = new PageResponse<AddressResponse>();
        MallUserAddrExample example = new MallUserAddrExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<MallUserAddr> mallUserAddrs =  mallUserAddrMapper.selectByExample(example);
        response.setData(mallUserAddrs.stream().map(o ->{
            AddressResponse vo = new AddressResponse();
            vo.setAddressId(o.getAddrId());
            vo.setProvId(o.getProvId());
            vo.setProvName(o.getProvName());
            vo.setCityId(o.getCityId());
            vo.setCityName(o.getCityName());
            vo.setAreaId(o.getAreaId());
            vo.setAreaName(o.getAreaName());
            vo.setImDefault(o.getImDefault());
            vo.setReceiveAddress(o.getAddressDetail());
            vo.setReceiveName(o.getReceiveName());
            vo.setReceiveTelephone(o.getReceiveTelephone());
            return vo;
        }).collect(Collectors.toList()));
        return response.success();
    }

    /**
     * 查询单个地址
     * @author 郭尔
     * @date 2018/10/25 10:30
     */
    public AddressDetailResponse selAddressById(Long addressId){
        MallUserAddr mallUserAddr = mallUserAddrMapper.selectByPrimaryKey(addressId);
        AddressDetailResponse response = new AddressDetailResponse();
        response.setAddressId(mallUserAddr.getAddrId());
        response.setReceiveAddress(mallUserAddr.getAddressDetail());
        response.setReceiveName(mallUserAddr.getReceiveName());
        response.setReceiveTelephone(mallUserAddr.getReceiveTelephone());

        response.setProvName(mallUserAddr.getProvName());
        response.setCityName(mallUserAddr.getCityName());
        response.setAreaName(mallUserAddr.getAreaName());
        response.setProvId(mallUserAddr.getProvId());
        response.setCityId(mallUserAddr.getCityId());
        response.setAreaId(mallUserAddr.getAreaId());
        response.setImDefault(mallUserAddr.getImDefault());
        return response.success();
    }

    /**
     * 用户的默认地址
     * @author 郭尔
     * @date 2018/10/25 10:39
     */
    public AddressResponse myDefaultAddress(Long userId){
        MallUserAddrExample example = new MallUserAddrExample();
        example.createCriteria().andUserIdEqualTo(userId).andImDefaultEqualTo(true);
        List<MallUserAddr> mallUserAddrs = mallUserAddrMapper.selectByExample(example);
        AddressResponse response = new AddressResponse();
        if(mallUserAddrs.size()>0){
            MallUserAddr mallUserAddr = mallUserAddrs.get(0);
            response.setProvId(mallUserAddr.getProvId());
            response.setCityId(mallUserAddr.getCityId());
            response.setAreaId(mallUserAddr.getAreaId());
            response.setProvName(mallUserAddr.getProvName());
            response.setCityName(mallUserAddr.getCityName());
            response.setAreaName(mallUserAddr.getAreaName());
            response.setAddressId(mallUserAddr.getAddrId());
            response.setReceiveAddress(mallUserAddr.getAddressDetail());
            response.setReceiveName(mallUserAddr.getReceiveName());
            response.setReceiveTelephone(mallUserAddr.getReceiveTelephone());
            response.setImDefault(mallUserAddr.getImDefault());
        }
        return response.success();
    }

}
